package com.bicycle.rentalsystem.servelt.admin;

import com.bicycle.rentalsystem.pojo.admin.Page;
import com.bicycle.rentalsystem.pojo.admin.Station;
import com.bicycle.rentalsystem.service.admin.StationService;
import com.bicycle.rentalsystem.service.impl.admin.StationServiceImpl;
import com.bicycle.rentalsystem.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Third uncle
 * @date 2022/11/27 - 16:44
 */
@WebServlet("/stationServlet")
public class StationServlet extends BaseServlet {
    StationService stationService=new StationServiceImpl();
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String pageNo = req.getParameter("pageNo");
        /*if(pageNo==null||pageNo==""){
            pageNo="1";
        }
        Integer pageNo2= Integer.valueOf(pageNo);*/
        int pageNo2 = WebUtil.parseInt(pageNo, 1);
        int pageSize = WebUtil.parseInt(req.getParameter("pageSize"), 4);
        //获取page对象
        Page<Station> page = stationService.page(pageNo2, 2);
        //保存到request域中
        req.setAttribute("page",page);
        //请求转发
        req.getRequestDispatcher("admin/a2.jsp").forward(req,resp);

    }
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数
        String stationName = req.getParameter("zhandianming");
        //2.保存到数据库
        Station station = new Station(null, stationName);
        stationService.addStation(station);
        //重定向回去
        resp.sendRedirect(req.getContextPath()+"/stationServlet?action=page");

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取要删除的id
        String id = req.getParameter("id");
        int aId = Integer.parseInt(id);
        //2.数据库中删除
        stationService.deleteStation(aId);
        //3.重定向回去
        resp.sendRedirect(req.getContextPath()+"/stationServlet?action=page");
    }

}
